/*****************************************************************
 * Author: Goh Chee Hong
 * Date of creation: 1/Oct/2008
 * Last modification: 1/Oct/2008
 * 
 * Description: Provide means to obtain and manage the subscription
 * 

 ****************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Pbdms.DataAccess.DataProvider
{
    /// <summary>
    /// a Subscription provider manage performing transactions to database (read/write)
    /// </summary>
    public class SubscriptionInfoProvider
    {
        const string FILENAME = "subscription.xml";
        /// <summary>
        /// create a SubscriptionProvider object
        /// </summary>
        /// <returns></returns>
        public static SubscriptionInfoProvider create()
        {
            return new SubscriptionInfoProvider();
        }
        /// <summary>
        /// get the list of all subscriptions from the database
        /// </summary>
        /// <returns></returns>
        public ArrayList getListOfSubscriptionInfo()
        {
            ArrayList arlResult = new ArrayList();
        
                
        /// </summary>
            ArrayList arlChannel= XmlHelper.create("subscription.xml").selectMultipleElementValues("/root/subscription/channel");

                ArrayList arlId = XmlHelper.create("subscription.xml").selectMultipleAttributeValues("/root/subscription", "id");
                ArrayList arlOriginator = XmlHelper.create("subscription.xml").selectMultipleElementValues("/root/subscription/friendID");
                int index = 0;
                if (arlId != null && arlId.Count > 0)
                {
                    foreach (object id in arlId)
                    {
                        Common.DataObject.SubscriptionInfo subscr = new Pbdms.Common.DataObject.SubscriptionInfo(int.Parse(arlId[index].ToString()), arlChannel[index].ToString(), arlOriginator[index].ToString());
                        arlResult.Add(subscr);
                        index++;
                    }
                }
                
         

            return arlResult;
        }
        /// <summary>
        /// update a subscription to database
        /// </summary>
        /// <param name="bdd"></param>
        public void updateSubscriptionInfoToDB(Common.DataObject.SubscriptionInfo subscr)
        {

            XmlHelper.create(FILENAME).updateTypedElement("/root/subscription[@id='" + subscr.ID + "']" + "/channel", subscr.Channel);
            XmlHelper.create(FILENAME).updateTypedElement("/root/subscription[@id='" + subscr.ID + "']" + "/friendID", subscr.FriendID);
        }

        /// <summary>
        /// append a new Subscription to the database
        /// </summary>
        /// <param name="bdd"></param>
        public void appendSubscriptionInfoToDB(Common.DataObject.SubscriptionInfo subscr)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<subscription id='" + subscr.ID + "'>");
            sb.Append("<channel>" + subscr.Channel + "</channel>");

            sb.Append("<friendID>" + subscr.FriendID + "</friendID>");
            sb.Append("</subscription>");
            XmlHelper.create(FILENAME).appendChild("/root",sb.ToString());
        }
        /// <summary>
        /// delete a Subscription from the database
        /// </summary>
        /// <param name="bdd"></param>
        public void deleteSubscriptionInfoFromDB(Common.DataObject.SubscriptionInfo subscr)
        {
            XmlHelper.create(FILENAME).deleteSelf("/root/subscription[@id='" + subscr.ID + "']");
        }
        public Common.DataObject.SubscriptionInfo selectASubscriptionInfoBasedOnIDFromDB(string id)
        {
            Common.DataObject.SubscriptionInfo subscription = new Pbdms.Common.DataObject.SubscriptionInfo();
            subscription.ID = int.Parse(id);
            subscription.Channel = XmlHelper.create(FILENAME).getInnerXML("/root/subscription[@id='" + id + "']/channel");
            subscription.FriendID= XmlHelper.create(FILENAME).getInnerXML("/root/subscription[@id='" + id + "']/friendID");
            return subscription;
        }
        public string selectIDBasedOnChannelAndFriend(string channel, string friendID)
        {



            string temp = XmlHelper.create(FILENAME).getElement("/root/subscription[pre:channel='" + channel + "' and pre:friendID='" + friendID + "']"); //
            return temp;
        }

    }
}
